//
//  ACProgressHUD.m
//  ACFramework
//
//  Created by caoyq on 2018/6/8.
//  Updated by caoyq on 2018/11/26.

#import "ACProgressHUD.h"
#import "MBProgressHUD.h"

@interface MBProgressHUD (ACExt)

@end

@implementation MBProgressHUD (ACExt)

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return nil;
}

@end

static NSString *const successImageName = @"ACFramework.bundle/ACProgressHUDSuccess@2x";
static NSString *const errorImageName   = @"ACFramework.bundle/ACProgressHUDError@2x";

@implementation ACProgressHUD

+ (instancetype)manager
{
    static ACProgressHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        instance.duration = 1.2;
        
        NSString *successPath = [[NSBundle bundleForClass:self] pathForResource:successImageName ofType:@"png"];
        UIImage *successImage = [[UIImage imageWithContentsOfFile:successPath] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        
        NSString *errorPath = [[NSBundle bundleForClass:self] pathForResource:errorImageName ofType:@"png"];
        UIImage *errorImage = [[UIImage imageWithContentsOfFile:errorPath] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        instance.successImage = successImage;
        instance.errorImage = errorImage;
        instance.hudView = [UIApplication sharedApplication].windows.lastObject;
    });
    
    return instance;
}

+ (UIImage *)imageForResourcePath:(NSString *)path ofType:(NSString *)type inBundle:(NSBundle *)bundle {
    return [UIImage imageWithContentsOfFile:[bundle pathForResource:path ofType:type]];
}

#pragma mark - hud

+ (void)showHUD
{
    [self showHUDToView:nil];
}

+ (void)showHUDToView: (UIView *)toView
{
    if (!toView) {
        toView = [ACProgressHUD manager].hudView;
    }
    [MBProgressHUD showHUDAddedTo:toView animated:YES];
}

+ (void)showHUDWithText: (NSString *)text
{
    [self showHUDWithText:text ToView:nil];
}

+ (void)showHUDWithText: (NSString *)text ToView: (UIView *)toView
{
    if (!toView) {
        toView = [ACProgressHUD manager].hudView;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    hud.label.text = text;
}

+ (void)hideHUD
{
    [self hideHUDFromView:nil];
}

+ (void)hideHUDFromView: (UIView *)fromView
{
    if (!fromView) {
        fromView = [ACProgressHUD manager].hudView;
    }
    [MBProgressHUD hideHUDForView:fromView animated:YES];
}

#pragma mark - text

+ (void)showText: (NSString *)text
{
    [self showText:text position:ACProgressHUDPositionMidden];
}

+ (void)showText: (NSString *)text toView: (UIView *)toView
{
    [self showText:text toView:toView position:ACProgressHUDPositionMidden];
}

+ (void)showText:(NSString *)text position: (ACProgressHUDPosition)position
{
    [self showText:text toView:nil position:position];
}

+ (void)showText:(NSString *)text toView: (UIView *)toView position: (ACProgressHUDPosition)position
{
    [self showText:text toView:toView position:position duration:0 icon:nil];
}

#pragma mark - success

+ (void)showSuccessWithText: (NSString *)text
{
    [self showSuccessWithText:text toView:nil];
}

+ (void)showSuccessWithText: (NSString *)text toView: (UIView *)toView
{
    [self showText:text toView:toView position:ACProgressHUDPositionMidden duration:0 icon:[ACProgressHUD manager].successImage];
}

#pragma mark - error

+ (void)showErrorWithText: (NSString *)text
{
    [self showErrorWithText:text toView:nil];
}

+ (void)showErrorWithText: (NSString *)text toView: (UIView *)toView
{
    [self showText:text toView:toView position:ACProgressHUDPositionMidden duration:0 icon:[ACProgressHUD manager].errorImage];
}

#pragma mark - base

/**
 最基本的一个方法

 @param text     提示文字
 @param toView   加载的view
 @param position 位置
 @param duration 持续时间
 @param icon     图标
 */
+ (void)showText:(NSString *)text toView: (UIView *)toView position: (ACProgressHUDPosition)position duration: (CGFloat)duration icon: (UIImage *)icon
{
    if (!toView) {
        toView = [ACProgressHUD manager].hudView;
    }
    if (!duration) {
        duration = [ACProgressHUD manager].duration;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    hud.label.text = text;
    
    if (icon) {
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:icon];
    }else {
        hud.mode = MBProgressHUDModeText;
    }
    
    if (position == ACProgressHUDPositionBottom) {
        hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    }else if (position == ACProgressHUDPositionTop) {
        hud.offset = CGPointMake(0.f, -MBProgressMaxOffset);
    }
    
    [hud hideAnimated:YES afterDelay:duration];
}

@end
